$(function () {
    var _main = $(".main");
    var _fanshow_id = getUrlParam("id");
    var _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    var _contentheight = ($(window).height() - CONFIG_MANAGER.headerHight);
    coindetailPage = function () {
        this.init();
    };
    coindetailPage.prototype = {
        // 初始化页面
        init: function () {
            this.loadData();
        },

        // 加载数据
        loadData: function () {
            var memberid = (_currentloginuser) ? _currentloginuser.id : 0;
            var model = this;
            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/fans/detail/{0}/{1}".format(_fanshow_id, memberid),
                beforeSend: function () {
                    $.showWaiting();
                }
            }).done(function (res) {
                model.paintpage(res);
                model.bindEvent();
            }).always(function (res) {
                $.closeWaiting();
            });
        },

        // 画页面
        paintpage: function (res) {
            var fans_show = res.fans_show;
            var html = [];
            html.push("<div class='banner'><div class='slider'><ul>");
            html.push("");
            if (fans_show.albums && fans_show.albums.length > 0) {
                $.each(fans_show.albums, function (i, item) {
                    html.push("<li><img class='lazy'  src='../../images/place/1080x1044.png' data-original='" + getImageUrl(item.photo, 1080, 1044) + "' /></li>");
                });
            } else {
                html.push("<li><img class='lazy' data-original='../../images/grey.gif' style='width: 1080px;height: 261px' /></li>");
            }
            html.push("</ul></div>");
            if (fans_show.albums && fans_show.albums.length > 0) {
                html.push("<p class='tups' id='tups'>1/" + fans_show.albums.length + "</p>");
            }
            html.push("</div>");

            html.push("<div class='muokuaibg mbtopbottom'>");
            html.push("<p class='xfxxqbt mbbottom'>" + fans_show.memo + "</p>");

            html.push("<div class='zansx'>");

            var fontcolor = res.is_praise == 1 ? "#f4514e" : "#000000";
            var praisestyle = res.is_praise == 1 ? "iconfont icon-lovefill" : "iconfont icon-love";
            html.push("<span class='zanan' id='btnpraise' style='color: " + fontcolor + "'><i class='" + praisestyle + "'></i><i>赞</i></span>");
            html.push("<ul class='zantx'>");
            if (fans_show.lst_praise_avatar) {
                var avatarlist = fans_show.lst_praise_avatar.split('|');
                $.each(avatarlist, function (i, img) {
                    html.push("<li><a><img class='lazy' data-original='" + getImageUrl(img) + "' /></a></li>");
                })
            }
            html.push("<li class='zanshul'><a><label  id='praisecount'>" + res.praise_count + "</label>个赞</a></li>");
            html.push("</ul></div></div>");
            html.push("<div class='muokuaibg mbtopbottom mtop5'>");
            html.push("<h2 class='xfxianbt mbbottom'>星发现<span>(" + fans_show.comments.length + ")</span></h2>");
            $.each(fans_show.comments, function (i, item) {
                html.push("<div class='mbbottom xfpinglun'>");
                html.push("<p class='pltxmzsj'><b><a><img src='" + getImageUrl(item.avatar, 82, 82) + "' /></a></b>" + item.member_name + "<span>" + dateDiff(new Date(item.create_time_text).getTime()) + "</span></p>");
                html.push("<p class='plnr'>" + item.content + "</p>");
                html.push("</div>");
            });
            html.push("</div>");
            $("#detailinfo").html(html.join(""));

            // 滚动广告
            $("#tups").text("1/" + $(".slider li").length);
            $(".slider").touchSlider({
                width: 375,
                height: 363,
                during: 3000,
                autoplay: false,
                showindr: false,
                move: function (currindex, total) {
                    $("#tups").text((currindex + 1) + "/" + total);
                }
            });
            $('img.lazy').lazyload({
                placeholder: "../../images/grey.gif",
                event: "scroll",
                threshold: 100,
                container: $(".main"),
                failure_limit: 10
            });
        },

        // 点赞
        praise: function (ispraise) {
            var postdata = {
                fanshow_id: _fanshow_id,
                praise: ispraise,
                to_member_id: _currentloginuser.id,
                to_member_name: _currentloginuser.name
            };
            $.ajax({
                headers: {'Content-Type': 'application/json', token: _currentloginuser.token},
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/fans/comment",
                data: JSON.stringify(postdata)
            }).done(function (data) {
                var praisecount = parseInt($("#praisecount").text());
                if (ispraise == 1) {
                    praisecount++;
                } else {
                    praisecount--;
                }
                $("#praisecount").text(praisecount);
            }).fail(function (res) {
                var response = JSON.parse(res.responseText);
                if (response.Message && response.Message.indexOf("票据") > -1) {
                    $.alert("你的帐号已经在其他地方登录", function () {
                        clearLocalStorage();
                        redirect("../account/login.html");
                        return false;
                    });
                }
            });
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 置顶
            $("#gotop").on("tap", function () {
                _main.animate({scrollTop: '0px'}, 500);
            });
            _main.scroll(this.isDisplayTopIcon);

            // 后退
            $("#btnback").on("tap", function () {
                back();
            });

            // 点赞
            $("#btnpraise").on("tap", function () {
                var ispraise = 0;
                var current = $(this).find("i");
                if (current.hasClass('icon-love')) {
                    ispraise = 1;
                    // 收藏
                    current.css("color", "#f4514e")[0].className = "iconfont icon-lovefill";
                } else {
                    ispraise = 0;
                    // 取消收藏
                    current.css("color", "#000000")[0].className = "iconfont icon-love";
                }

                model.praise(ispraise);

            });
            //
        },

        // 判断显示置顶图标
        isDisplayTopIcon: function () {
            var top = _main.scrollTop();
            if (top > _contentheight / 2) {
                $("#gotop").show();
            } else {
                $("#gotop").hide();
            }
        },
    };

    var coindetailPage = new coindetailPage();
});

// 提交评价
function commitComment(content) {
    var _fanshow_id = getUrlParam("id");
    var _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    if (!_currentloginuser) {
        redirect("../account/login.html");
        return false;
    }
    if (!(content && content.trim())) {
        return false;
    }
    var postdata = {
        fanshow_id: _fanshow_id,
        comment: content,
        to_member_id: _currentloginuser.id,
        to_member_name: _currentloginuser.name
    };
    var detailajax = $.ajax({
        headers: {'Content-Type': 'application/json', token: _currentloginuser.token},
        type: "POST",
        url: CONFIG_MANAGER.RequstUrl + "v1/fans/comment",
        data: JSON.stringify(postdata)
    }).done(function (data) {

    }).fail(function (res) {
        var response = JSON.parse(res.responseText);
        if (response.Message && response.Message.indexOf("票据") > -1) {
            $.alert("你的帐号已经在其他地方登录", function () {
                clearLocalStorage();
                redirect("../account/login.html");
                return;
            });
        }
    });
}